*! This file demonstrates how to conduct specification tests on various SF models.

*! Citation:
*! Chen, Y.-T., and Wang, H.-J. (2012) "Centered-Residuals-Based Moment Estimator
*! and Test for Stochastic Frontier Models", Econometric Reviews 31(6), pp.625-653.


set trace off
myclear
set more off
set matsize 1000

capture log close
log  using demo_tests_log.txt, text replace


**************************************
use milk
************************************

  /****** define explanatory varibles ******/

global xvar llabor lfeed lcattle lland

********************************************************
****** The Skewness Test (Schmidt and Lin, 1984) *******
********************************************************

reg ly $xvar

mat b_ols = e(b)      /* save the coefficient vector */
scalar ll_ols = e(ll) /* save the log liklihood value */


predict e, resid /* the OLS errors are saved in e */
sum e, detail
sktest e, noadj


***************************************
****** Half-Normal model, homo. *******
***************************************

sfmodel ly, prod dist(h) frontier($xvar) usigmas() vsigmas() show
sf_srch, n(1) frontier($xvar)  fast nograph
ml max, difficult gradient gtol(1e-5) nrtol(1e-5)

mat b_h = e(b)        /* save the coefficient vector */
scalar ll_h = e(ll) /* save the log likelihood value */

   /* --------- LR Test on the existence of u ----- */

di -2*(ll_ols - ll_h)
sf_mixtable, dof(1)

   /* --------- Specification Test: moment-based test (Chen and Wang 2012) ---- */
   /* --------- Null: The model specification is correct. -------- */

   sfmtest ly, production frontier($xvar) udist(h) omega(1)

       *** with different values of omega ****

     forvalues i=0.5(0.5)2{
        sfmtest ly, production frontier($xvar) udist(h) omega(`i')
     }
